<template>
    <div class="weather-container" v-if="currentWeather.city">
        <div class="header">
            <p>
                你正在预览{{ currentWeather.city }}的天气信息<span v-if="spanShow"
          >,可以通过右上角的"+"号按钮保存起来</span>
            </p>
        </div>
        <div class="wrap content">
            <div class="weather-detail">
                 <p>当日的气温是: {{ currentWeather.temperature }}摄氏度</p>
                 <p>当日的天气是: {{ currentWeather.weather }}</p>
                 <p>当日风向是: {{ currentWeather.winddirection }}风</p>
                 <p>当日风力是: {{ currentWeather.windpower }}级</p>
            </div>
            <Charts
            :weatherInFourDays="cityWeatherInFourDaysSearch"
            :showTitle="false"
            />
        </div>
    </div>
    <div class="loading" v-else>
        <h1>天气信息加载中...</h1>
    </div>
</template>

<script setup>
import { ref,computed,watch } from 'vue'
import { useRoute } from 'vue-router';
import { getBaseWeatherByAdcode,getAllWeatherByAdcode } from '@/api/index.js';
import { WeatherStore } from '@/stores/counter';
import { storeToRefs } from 'pinia';
import Charts from '@/components/Charts.vue';

const weatherStore = WeatherStore()
const { cityWeatherList } = storeToRefs(weatherStore)

const route = useRoute()
const currentWeather = ref({})
const cityWeatherInFourDaysSearch = ref([])

//"添加当前城市"的span是否显示
const spanShow = computed(() => {
    return !cityWeatherList.value.find(
        (item) => item.city === route.query.search
    )
})

//获取城市天气信息
watch(
    route,
    async () => {
        const res = await getBaseWeatherByAdcode(route.params.Adcode)
        currentWeather.value = res.lives[0];
        const res2 = await getAllWeatherByAdcode(route.params.Adcode)
        cityWeatherInFourDaysSearch.value = res2.forecasts[0].casts
    },
    { immediate:true }
)
</script>

<style lang="scss" scoped>
.weather-container {
    height: 90vh;
    color: #fff;
    background-color:#00668a;
    .header {
        width: 100%;
        background-color: #004e71;
        text-align: center;
        padding: 8px;
    }
    p {
        font-size: 1rem;
        margin: 10px;
    }
    .content {
        .weather-detail {
            display: flex;
            flex-direction: column;
            gap: 5px;
            align-items: center;
            padding: 5px;
        }
    }
}

.loading {
    height: 90vh;
    color: #fff;
    background-color: #00668a;
    h1 {
        font-size: 20px;
        padding-top: 20px;
        text-align: center;
    }
}
</style>